package Homework09;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;

public class Homework {
    public static void main(String[] args) {
        String filePath = "决策引擎Java-data.txt";
        List<String> lines = readLinesFromFile(filePath);

        if (lines.isEmpty()) {
            System.out.println("No data found in the file.");
            return;
        }

        String hoursLine = lines.get(1);
        String halfHourLine = lines.get(3);
        String quarterHourLine = lines.get(5);

        List<Integer> hours = parseHours(hoursLine);
        List<List<Integer>> halfHourData = parseHalfHourData(halfHourLine);
        List<List<Integer>> quarterHourData = parseQuarterHourData(quarterHourLine);

        Map<String, Integer> halfHourMap = buildHalfHourMap(hours, halfHourData);
        Map<String, Integer> quarterHourMap = buildQuarterHourMap(hours, quarterHourData);

        System.out.println("小时：");
        System.out.println(hoursLine);
        System.out.println("30分钟：");
        System.out.println(halfHourLine);
        System.out.println("15分钟：");
        System.out.println(quarterHourLine);

        System.out.println("半小时数据:");
        printMap(halfHourMap);
        System.out.println("半小时排序数据:");
        printMap(sortByValueDescending(halfHourMap));

        System.out.println("15分钟数据:");
        printMap(quarterHourMap);
        System.out.println("15分钟排序数据:");
        printMap(sortByValueDescending(quarterHourMap));
    }

    private static List<String> readLinesFromFile(String filePath) {
        try {
            return Files.readAllLines(Paths.get(filePath));
        } catch (IOException e) {
            e.printStackTrace();
            return Collections.emptyList();
        }
    }

    private static List<Integer> parseHours(String line) {
        return Arrays.stream(line.split(";"))
                     .map(Integer::parseInt)
                     .collect(Collectors.toList());
    }

    private static List<List<Integer>> parseHalfHourData(String line) {
        return Arrays.stream(line.split(";"))
                     .map(segment -> Arrays.stream(segment.split(","))
                                           .map(entry -> Integer.parseInt(entry.split(":")[1]))
                                           .collect(Collectors.toList()))
                     .collect(Collectors.toList());
    }

    private static List<List<Integer>> parseQuarterHourData(String line) {
        return Arrays.stream(line.split(";"))
                     .map(segment -> Arrays.stream(segment.split(","))
                                           .map(entry -> Integer.parseInt(entry.split(":")[1]))
                                           .collect(Collectors.toList()))
                     .collect(Collectors.toList());
    }

    private static Map<String, Integer> buildHalfHourMap(List<Integer> hours, List<List<Integer>> data) {
        Map<String, Integer> map = new LinkedHashMap<>();
        for (int i = 0; i < hours.size(); i++) {
            int hour = hours.get(i);
            List<Integer> halfHourValues = data.get(i);
            map.put(String.format("<%02d30,%d>", hour, halfHourValues.get(0)), halfHourValues.get(0));
            map.put(String.format("<%02d60,%d>", hour, halfHourValues.get(1)), halfHourValues.get(1));
        }
        return map;
    }

    private static Map<String, Integer> buildQuarterHourMap(List<Integer> hours, List<List<Integer>> data) {
        Map<String, Integer> map = new LinkedHashMap<>();
        for (int i = 0; i < hours.size(); i++) {
            int hour = hours.get(i);
            List<Integer> quarterHourValues = data.get(i);
            map.put(String.format("<%02d15,%d>", hour, quarterHourValues.get(0)), quarterHourValues.get(0));
            map.put(String.format("<%02d30,%d>", hour, quarterHourValues.get(1)), quarterHourValues.get(1));
            map.put(String.format("<%02d45,%d>", hour, quarterHourValues.get(2)), quarterHourValues.get(2));
            map.put(String.format("<%02d60,%d>", hour, quarterHourValues.get(3)), quarterHourValues.get(3));
        }
        return map;
    }

    private static Map<String, Integer> sortByValueDescending(Map<String, Integer> map) {
        return map.entrySet()
                  .stream()
                  .sorted((e1, e2) -> e2.getValue().compareTo(e1.getValue()))
                  .collect(Collectors.toMap(
                      Map.Entry::getKey,
                      Map.Entry::getValue,
                      (e1, e2) -> e1,
                      LinkedHashMap::new
                  ));
    }

    private static void printMap(Map<String, Integer> map) {
        map.forEach((key, value) -> System.out.print(key + " "));
        System.out.println();
    }
}

